<template>
	<view class="index-page">
		<u-navbar :background="background" title-size="36" :is-back="false" height="42" :border-bottom="false"
			title="产品"></u-navbar>
		<view class="index-search-bar ">
			<view class="search-box">
				<image src="../../static/images/search-2.png" mode="" class="search-icon"></image>
				<view class="flex_bd">
					<input type="text" value="" placeholder="请输入产品名称/型号" />
				</view>
				<view class="reset-link">重置</view>
			</view>
		</view>
		<view class="sticky-filter">
			<u-dropdown ref="uDropdown" active-color="#A2C833" menu-icon="arrow-down-fill" menu-icon-size="0" height="90">
				<u-dropdown-item title="全部" v-model="value1">
					<view class="slot-content" style="background-color: #FFFFFF;">
						<view class="u-menu-wrap">
							<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view"
								:scroll-top="scrollTop">
								<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
									:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
									@tap.stop="swichMenu(index)">
									<text class="u-line-1">{{item.name}}</text>
								</view>
							</scroll-view>
							<block v-for="(item,index) in tabbar" :key="index">
								<scroll-view scroll-y class="right-box flex_bd" v-if="current==index">
									<view class="page-view">
										<view class="class-item">
											<view class="item-title">
												<text>{{item.name}}</text>
											</view>
											<view class="item-container">
												<view class="thumb-box" v-for="(item1, index1) in item.foods"
													:key="index1">
													<view class="item-menu-name" @click="closeDropdown">{{item1.name}}</view>
												</view>
											</view>
										</view>
									</view>
								</scroll-view>
							</block>
						</view>
					</view>
				</u-dropdown-item>
				<u-dropdown-item title="佣金排序" v-model="value2">
					<view class="slot-content" style="background-color: #FFFFFF;"></view>
				</u-dropdown-item>
			</u-dropdown>
			<!-- <view class="filter-item">
				<text>全部</text>
				<image class="icon" src="../../static/images/down-arrow.png" mode=""></image>
			</view>
			<view class="filter-item">
				<text>佣金排序</text>
				<image class="icon" src="../../static/images/filter-arrow.png" mode=""></image>
			</view> -->
		</view>
		<view class="list-main">
			<view class="product-list" v-for="(item,index) in 4">
				<view class="product-cell flex-center" @click="openUrl('/pages/product/detail')">
					<view class="photo">
						<image src="../../static/images/indexpic.jpg" mode=""></image>
					</view>
					<view class="flex_bd">
						<view class="title">
							<view class="flex_bd ellipsis">ZE60E-10 履带式液压挖掘机</view>
							<image v-if="index == 0" src="../../static/images/hot.png" class="hot-icon" mode=""></image>
						</view>
						<view class="item">操作质量：6050kg</view>
						<view class="item">标准斗容：0.23m²</view>
						<view class="item">预计佣金：<text>5000元/台</text></view>
					</view>
				</view>
				<view class="product-foot">
					<view class="bar-left">
						<image src="../../static/images/audio.png" mode=""></image><text>6543人分享，543人推荐</text>
					</view>
					<view class="bar-right">
						<view class="bar-link" @click="openUrl('/pages/product/share')">分享</view>
						<view class="bar-link">推荐</view>
					</view>
				</view>
			</view>
		</view>
		<u-tabbar inactive-color="#C8C8CC" active-color="#9DC851" v-model="footcurrent" :list="tabbarlist"
			:mid-button="true" @change="barchange"></u-tabbar>
	</view>
</template>
<script>
	import classifyData from '@/common/classify.data.js';
	export default {
		data() {
			return {
				// tab
				tabbarlist: '',
				footcurrent: 1,
				background: {
					backgroundColor: '#A2C833',
				},
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				tabbar: [{
					name: '全部',
					foods: [{
						"name": "全部",
						"key": "全部",
					}, {
						"name": "混凝土泵车",
						"key": "混凝土泵车",
					}, {
						"name": "搅拌车",
						"key": "搅拌车",
					}, {
						"name": "履带式挖掘机",
						"key": "履带式挖掘机",
					}, {
						"name": "塔式起重机",
						"key": "塔式起重机",
					}]
				}, {
					name: '挖掘机',
					foods: [{
						"name": "全部",
						"key": "全部",
					}, {
						"name": "混凝土泵车",
						"key": "混凝土泵车",
					}, {
						"name": "搅拌车",
						"key": "搅拌车",
					}, {
						"name": "履带式挖掘机",
						"key": "履带式挖掘机",
					}, {
						"name": "塔式起重机",
						"key": "塔式起重机",
					}]
				}, {
					name: '混凝土机械',
					foods: [{
						"name": "全部",
						"key": "全部",
					}, {
						"name": "混凝土泵车",
						"key": "混凝土泵车",
					}, {
						"name": "搅拌车",
						"key": "搅拌车",
					}, {
						"name": "履带式挖掘机",
						"key": "履带式挖掘机",
					}, {
						"name": "塔式起重机",
						"key": "塔式起重机",
					}]
				}],
				menuItemPos: [],
				arr: [],
				scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
				timer: null, // 定时器
				value1:'',
				value2:''
			}
		},
		onLoad() {
			this.tabbarlist = [{
					iconPath: "/static/images/foot1.png",
					selectedIconPath: "/static/images/foot1_on.png",
					text: '首页',
					isDot: false,
					customIcon: false,
					pagePath: "/pages/index/index"
				},
				{
					iconPath: "/static/images/foot2.png",
					selectedIconPath: "/static/images/foot2_on.png",
					text: '产品',
					customIcon: false,
					pagePath: "/pages/product/product"
				},
				{
					iconPath: "/static/images/middle.png",
					selectedIconPath: "/static/images/middle.png",
					midButton: true,
					customIcon: false,
					pagePath: "/pages/clue/newClue"
				},
				{
					iconPath: "/static/images/foot3.png",
					selectedIconPath: "/static/images/foot3_on.png",
					text: '线索',
					customIcon: false,
					pagePath: "/pages/clue/clue"
				},
				{
					iconPath: "/static/images/foot4.png",
					selectedIconPath: "/static/images/foot4_on.png",
					text: '我的',
					isDot: false,
					customIcon: false,
					pagePath: "/pages/ucenter/ucenter"
				},
			]
		},
		methods: {
			openUrl(e) {
				console.log(e)
				uni.navigateTo({
					url: e
				});
			},
			change(index) {
				this.current = index;
			},
			barchange(e) {
				if (e == 2) {
					uni.navigateTo({
						url: '/pages/clue/newClue'
					});
				}
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			closeDropdown(){
				this.$refs.uDropdown.close();
			}
		}
	}
</script>

<style lang="scss">
	.index-search-bar {
		background: #A2C833;
		padding: 20rpx 30rpx 30rpx 30rpx;
	}

	.search-box {
		height: 72rpx;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		border-radius: 40rpx;
		background-color: #FFFFFF;

		.search-icon {
			margin-right: 10rpx;
			width: 40rpx;
			height: 40rpx;
			display: block;
		}

		.reset-link {
			font-size: 28rpx;
			padding: 0 20rpx;
		}
	}

	.list-main {
		padding: 20rpx 30rpx;
		height: calc(100vh - 242px);
		overflow: hidden;
		overflow-y: auto;
	}

	.product-list {
		padding: 30rpx 0;
		border-bottom: 1px solid #eeeeee;
	}

	.product-list:last-child {
		padding-bottom: 0;
		border-bottom: 0;
	}

	.product-cell {
		.photo {
			background: #E5E5E5;
			border-radius: 12px;
			font-size: 0;
			margin-right: 30rpx;

			image {
				width: 200rpx;
				height: 176rpx;
			}
		}

		.title {
			display: flex;
			align-items: center;
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 8rpx;
		}

		.item {
			font-size: 26rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #303337;
			line-height: 42rpx;

			text {
				color: #FE6714;
				font-weight: bold;
			}
		}
	}

	.product-foot {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;

		.bar-left {
			display: flex;
			align-items: center;
			padding: 0 8rpx;
			height: 52rpx;
			font-size: 24rpx;
			color: #606366;
			background: linear-gradient(270deg, rgba(157, 200, 81, 0) 0%, rgba(157, 200, 81, 0.3) 100%);

			image {
				margin-right: 8rpx;
				width: 32rpx;
				height: 38rpx;
			}
		}

		.bar-right {
			width: 306rpx;
			height: 52rpx;
			line-height: 52rpx;
			background: #9DC851;
			font-size: 26rpx;
			color: #FFFFFF;
			overflow: hidden;
			text-align: center;

			.bar-link {
				position: relative;
				height: 52rpx;
				line-height: 52rpx;
				width: 50%;
				float: left;

				&:first-child {
					background-color: #303337;
				}

				&:first-child::after {
					content: '';
					position: absolute;
					right: -10px;
					top: 0;
					bottom: 0;
					width: 0;
					height: 0;
					border-bottom: 52rpx solid #303337;
					border-right: 10px solid transparent;
				}
			}
		}
	}

	/deep/ .u-tabbar__content__circle__button {
		width: 136rpx !important;
		height: 136rpx !important;
	}

	/deep/ .u-tabbar__content__circle__button image {
		width: 136rpx !important;
		height: 136rpx !important;
	}

	/deep/ .u-tabbar__content__circle__border {
		display: none !important;
	}

	.sticky-filter {
		display: flex;
		align-items: center;
		width: 100%;
		height: 90rpx;
		background: #FFFFFF;
		border-bottom: 1px solid #EFEFEF;

		.filter-item {
			display: flex;
			align-items: center;
			justify-content: center;
			flex: 1;

			text {
				color: #606366;
				font-size: 28rpx;
			}

			.icon {
				width: 24rpx;
				height: 24rpx;
			}
		}
	}


	.u-menu-wrap {
		display: flex;
		overflow: hidden;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
		background: rgba(165, 160, 152, 0.2);
	}

	.u-tab-item {
		height: 92rpx;
		// background: rgba(165, 160, 152, 0.2);
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #8C8E90;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #303337;
		font-weight: 500;
		background: #fff;
	}


	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: #fff;
	}

	.class-item {
		background-color: #fff;
		padding: 30rpx;
	}

	.item-title {
		font-size: 32rpx;
		color: #303337;
		font-weight: 600;
		line-height: 44rpx;
		margin-bottom: 24rpx;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 28rpx;
		background-color: #F7F7F7;
		border-radius: 6rpx;
		color: #303337;
		padding: 0 24rpx;
		line-height: 60rpx;
		white-space: nowrap;
	}

	.thumb-box {
		display: inline-block;
		margin-right: 16rpx;
		margin-bottom: 24rpx;
	}
	
	/deep/ .u-dropdown__menu__item__arrow{
		width: 24rpx;
		height: 24rpx;
		background: url(../../static/images/down-arrow.png) no-repeat;
		background-size: contain;
	}
	
	/deep/ .u-dropdown__menu__item:last-child .u-dropdown__menu__item__arrow{
		width: 24rpx;
		height: 24rpx;
		background: url(../../static/images/filter-arrow.png) no-repeat;
		background-size: contain;
	}
	
	/deep/ .u-dropdown__content__popup{
		background-color: #fff;
		height: 100%;
	}
	
	.u-dropdown-item{
		height: 100%;
	}
	
	.slot-content{
		height: 100%;
	}
	
	.u-menu-wrap{
		height: 100%;
	}
</style>
